/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.bs.extensoesSwing;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JOptionPane;

/**
 *
 * @author Bone
 */
public class ExcluirRegistroGridButton extends PadraoGridButton {

    public ExcluirRegistroGridButton() {
        addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                int result = JOptionPane.showConfirmDialog(null, "Deseja realmente excluir este item?", "Exclusão de item", JOptionPane.YES_NO_OPTION);
                if (result == JOptionPane.NO_OPTION) {
                    return;
                }
                AbstractGridController c = getController();
                Object item = c.getBeanSelecionado();
                if (item != null) {
                    if (c.getIdBeanSelecionado() != null) {
                        if (!c.getEntityManager().getTransaction().isActive()) {
                            c.getEntityManager().getTransaction().begin();
                        }
                        c.getEntityManager().excluir(item);
                        c.getEntityManager().getTransaction().commit();
                    }
                    c.getLista().remove(item);
                }
            }
        });
        setText("Excluir");
        setPreferredSize(new Dimension(100, 45));
    }
}
